package com.ltx.blog_ltx.controller;


import com.alibaba.fastjson.JSONObject;
import com.ltx.blog_ltx.entity.dto.BlogDto;
import com.ltx.blog_ltx.entity.dto.RemarkDto;
import com.ltx.blog_ltx.entity.pojo.Blog;
import com.ltx.blog_ltx.entity.pojo.PageLike;
import com.ltx.blog_ltx.mapper.*;
import com.ltx.blog_ltx.until.FileUntil;
import com.ltx.blog_ltx.until.RemarkDtoTree;
import com.ltx.blog_ltx.until.jwtUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

@RestController
public class BlogController {

    @Autowired
    public BlogDtoMapper blogDtoMapper;
    @Autowired
    public BlogMapper blogMapper;
    @Autowired
    public UserMapper userMapper;
    @Autowired
    public CollectMapper collectMapper;
    @Autowired
    public RemarkDtoMapper remarkDtoMapper;
    @Autowired
    public RemarkMapper remarkMapper;
    @Autowired
    public PageLikeMapper pageLikeMapper;

    @PostMapping("/BlogTfno")
    public List<BlogDto> BlogIfno(@RequestBody JSONObject param){
        System.out.println("您成功进来了");
        int page = Integer.parseInt(param.getString("page"));
        int pageSize = Integer.parseInt(param.getString("pageSize"));
        System.out.println("页数是"+page+"页数的size是"+pageSize);
        List<BlogDto> blogDtos = blogDtoMapper.BlogInfoPage((page - 1) * pageSize, pageSize);
        Collections.sort(blogDtos);
        return blogDtos;
    }

    @PostMapping("/BlogIfnoByName")
    public List<BlogDto> BlogIfnoByName(@RequestBody JSONObject param){
        String user_token = param.getString("user_token");
        String tokenName = jwtUntil.getTokenName(user_token);
        int userId = userMapper.queryIdByName(tokenName);
        System.out.println(blogDtoMapper.BlogInfoById(userId));
        List<BlogDto> blogDtos = blogDtoMapper.BlogInfoById(userId);
        Collections.sort(blogDtos);
        return blogDtos;
    }

    @PostMapping("/delete_blog")
    public void delete_blog(@RequestBody JSONObject param){
        System.out.println("您进来了");
        int blogId = Integer.parseInt(param.getString("blogId"));
        System.out.println(blogId);
        blogMapper.deleteBlog(blogId);
    }

    @PostMapping("/rt_blognum")
    public int rt_blognum(){
        return blogMapper.blogNum();
    }

    @PostMapping("/blog_ifon")
    public BlogDto blog_ifon(@RequestBody JSONObject param){
        String blogId = param.getString("blogId");
        return blogDtoMapper.BlogBybId(Integer.parseInt(blogId));
    }

    @PostMapping("/blogger_info")
    public String blogger_info(@RequestBody JSONObject param){
        System.out.println("来了噢");
        String blogId = param.getString("blogId");
        System.out.println(blogId);
        int userId = blogDtoMapper.userIdBybid(Integer.parseInt(blogId));
        String blogName = userMapper.queryNameById(userId);
        System.out.println(userMapper.getUserHeadByName(blogName));
        return userMapper.getUserHeadByName(blogName);
    }

    @PostMapping("/addPageView")
    public void addPageView(@RequestBody JSONObject param){
        Integer blogId = param.getInteger("blogId");
        blogMapper.updatePageView(blogId);
    }


    /***
     * 点赞
     * @param param
     */
    @PostMapping("/GiveLike")
    public int GiveLike(@RequestBody JSONObject param){
        int blogId = param.getInteger("blogId");
        String user_token = param.getString("user_token");
        String userName = jwtUntil.getTokenName(user_token);
        int userId = userMapper.queryIdByName(userName);
        int Id = (int) ((Math.random() * 9 + 1) * 100000);
        PageLike pageLike = new PageLike(Id, blogId, userId);
        pageLikeMapper.Give_Like(pageLike);
        blogMapper.GiveLike(blogId);
        return blogMapper.LookLike(blogId);
    }

    /***
     * 取消点赞
     * @param param
     */
    @PostMapping("/CancelLike")
    public int CancelLike(@RequestBody JSONObject param){
        int blogId = param.getInteger("blogId");
        String user_token = param.getString("user_token");
        String userName = jwtUntil.getTokenName(user_token);
        int userId = userMapper.queryIdByName(userName);
        pageLikeMapper.unlike(blogId, userId);
        blogMapper.cancelLike(blogId);
        return blogMapper.LookLike(blogId);
    }

    @PostMapping("/isLike")
    public boolean isLike(@RequestBody JSONObject param){
        int blogId = param.getInteger("blogId");
        String user_token = param.getString("user_token");
        String userName = jwtUntil.getTokenName(user_token);
        int userId = userMapper.queryIdByName(userName);
        return pageLikeMapper.isLike(blogId, userId)!=0;
    }

    @PostMapping("/Collect_ifno")
    public List<BlogDto> Collect_ifno(@RequestBody JSONObject param){
        String user_token = param.getString("user_token");
        String tokenName = jwtUntil.getTokenName(user_token);
        int userId = userMapper.queryIdByName(tokenName);
        List<Integer> blogids = collectMapper.Collect_Blogid(userId);
        List<BlogDto> blogDtos = new ArrayList<>();
        for(int i=0;i<blogids.size();i++){
            int blogid = blogids.get(i);
            BlogDto blogDto = blogDtoMapper.BlogBybId(blogid);
            blogDtos.add(blogDto);
        }
        return blogDtos;
    }

    @PostMapping("/AllRemark")
    public List<RemarkDto> AllRemark(@RequestBody JSONObject param){
        Integer blogId = param.getInteger("blogId");
        List<RemarkDto> remarkDtos = remarkDtoMapper.BlogRemark(blogId);
        RemarkDtoTree remarkTreeUntil = new RemarkDtoTree(remarkDtos);
        remarkDtos = remarkTreeUntil.BuildTree();
        return remarkDtos;
    }

    @PostMapping("/SubmitComment")
    public RemarkDto  SubmitComment(@RequestBody JSONObject param){
        String user_token = param.getString("user_token");
        String tokenName = jwtUntil.getTokenName(user_token);
        int user_id = userMapper.queryIdByName(tokenName);
        Integer reply_id = param.getInteger("remarkId");
        Integer blogId = param.getInteger("blogId");
        String reply_name = param.getString("userName");
        String replyContent = param.getString("replyContent");
        int remarkId = (int) ((Math.random() * 9 + 1) * 100000);
        remarkMapper.AddRemark(remarkId,replyContent,blogId,user_id,reply_id,new Date(),reply_name);
        return  remarkDtoMapper.OneRemark(blogId, remarkId);
    }

    @PostMapping("/parentRemark")
    public  RemarkDto parentRemark(@RequestBody JSONObject param){
        String user_token = param.getString("user_token");
        String tokenName = jwtUntil.getTokenName(user_token);
        int user_id = userMapper.queryIdByName(tokenName);
        Integer blogId = param.getInteger("blogId");
        String parentContent = param.getString("parentContent");
        int remarkId = (int) ((Math.random() * 9 + 1) * 100000);
        remarkMapper.AddRemark(remarkId,parentContent,blogId,user_id,0,new Date(),null);
        return  remarkDtoMapper.OneRemark(blogId, remarkId);
    }

    @PostMapping("/toChange")
    public Map<String,Object> toChange(@RequestBody JSONObject param) throws FileNotFoundException {
        HashMap<String, Object> Map = new HashMap<>();
        Integer blogId = param.getInteger("blogId");
        String path = blogMapper.Bolg_url(blogId);
        String value;
        value=FileUntil.readFile(path);
        Map.put("value",value);
        String BlogTitle = blogMapper.queryBlogTitle(blogId);
        String SortName = blogMapper.querySort(blogId);
        System.out.println();
        Map.put("BlogTitle",BlogTitle);
        Map.put("SortName",SortName);
        return Map;
    }

    @PostMapping("/search")
    public List<BlogDto> search(@RequestBody JSONObject param){
        String input = param.getString("input");
        List<BlogDto> search = blogDtoMapper.search(input);
        return search;
    }

    @PostMapping("/BloggerCard")
    public List<BlogDto> BloggerCard(@RequestBody JSONObject param){
        String userName = param.getString("userName");
        int userId = userMapper.queryIdByName(userName);
        List<BlogDto> blogDtos = blogDtoMapper.BlogInfoById(userId);
        Collections.sort(blogDtos);
        return blogDtos;
    }
}
